import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { User } from './User';

export type TransactionType = 'purchase' | 'usage' | 'refund' | 'expiration';

@Entity('credit_transactions')
export class CreditTransaction {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'user_id' })
  userId: number;

  @Column({ 
    name: 'transaction_type',
    type: 'enum',
    enum: ['purchase', 'usage', 'refund', 'expiration']
  })
  transactionType: TransactionType;

  @Column({ name: 'credit_amount', type: 'int' })
  creditAmount: number;

  @Column({ name: 'description', type: 'text', nullable: true })
  description?: string;

  @Column({ name: 'reference_id', nullable: true })
  referenceId?: string; // 关联的支付ID或其他引用ID

  @Column({ name: 'metadata', type: 'json', nullable: true })
  metadata?: Record<string, any>;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @ManyToOne(() => User, (user) => user.creditTransactions)
  @JoinColumn({ name: 'user_id' })
  user: User;
} 